<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布者" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入发布者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['cc:review:add']"
        >发布活动</el-button>
      </el-col>
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="编号" align="center" width="60" :header-cell-style="{whiteSpace: 'nowrap'}" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="发布者" align="center" prop="merchantName" />
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <span :class="getStatusClass(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="详情" v-model="detailDialog" width="600px" append-to-body>
      <el-form :model="detailForm" label-width="100px">
        <el-form-item label="标题">
          <el-input v-model="detailForm.title" disabled />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="detailForm.content" type="textarea" disabled rows="6" />
        </el-form-item>
        <el-form-item label="发布者">
          <el-input v-model="detailForm.merchantName" disabled />
        </el-form-item>
        <el-form-item label="发布时间">
          <el-input v-model="detailForm.time" disabled />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-input v-model="detailForm.statusText" disabled />
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="detailForm.extend1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- 只有待审核状态才显示审核按钮 -->
          <template v-if="detailForm.status === 0">
            <el-button type="success" @click="handleApprove">审核通过</el-button>
            <el-button type="danger" @click="handleReject">审核不通过</el-button>
          </template>
          <el-button @click="detailDialog = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改待审核对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reviewRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发布人" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="发布时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Review">
import { ref, reactive, toRefs } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listReview, getReview, delReview, addReview, updateReview } from "@/api/cc/review"

const { proxy } = getCurrentInstance()

const reviewList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const detailDialog = ref(false)
const detailForm = reactive({})

const data = reactive({
  form: {},
  queryParams: {
      pageNum: 1,
      pageSize: 10,
      title: null,
      merchantName: null,
      orderByColumn: 'time',
      isAsc: 'desc'
    },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查看详情 */
function handleDetail(row) {
  // 设置详情表单数据
  Object.assign(detailForm, row)
  // 转换状态为文字
  detailForm.statusText = getStatusText(row.status)
  // 打开详情弹窗
  detailDialog.value = true
}

/** 审核通过 */
function handleApprove() {
  ElMessageBox.confirm('确认要审核通过该记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const submitData = {
      id: detailForm.id,
      status: 1, // 1表示审核通过
      isLaunch: 0, // 审核通过时额外将isLaunch设置为0
      extend1: detailForm.extend1
    }
    updateReview(submitData).then(() => {
      ElMessage.success('审核通过成功')
      // 更新弹窗中的状态
      detailForm.status = 1
      detailForm.statusText = getStatusText(1)
      // 重新加载列表
      getList()
    }).catch(error => {
      ElMessage.error('审核失败：' + error.message)
    })
  }).catch(() => {
    // 取消操作
  })
}

/** 审核不通过 */
function handleReject() {
  ElMessageBox.confirm('确认要审核不通过该记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const submitData = {
      id: detailForm.id,
      status: 2, // 2表示审核不通过
      extend1: detailForm.extend1
    }
    updateReview(submitData).then(() => {
      ElMessage.success('审核不通过成功')
      // 更新弹窗中的状态
      detailForm.status = 2
      detailForm.statusText = getStatusText(2)
      // 重新加载列表
      getList()
    }).catch(error => {
      ElMessage.error('审核失败：' + error.message)
    })
  }).catch(() => {
    // 取消操作
  })
}

/** 查询待审核列表 */
function getList() {
  loading.value = true
  listReview(queryParams.value).then(response => {
    reviewList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    uid: null,
    time: null,
    status: null,
    isLaunch: null,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    extend1: null,
    extend2: null
  }
  proxy.resetForm("reviewRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加待审核"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getReview(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改待审核"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reviewRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReview(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addReview(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除待审核编号为"' + _ids + '"的数据项？').then(function() {
    return delReview(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 获取审核状态文字 */
function getStatusText(status) {
  const statusMap = {
    0: '待审核',
    1: '审核通过',
    2: '审核不通过'
  }
  return statusMap[status] || '未知状态'
}

/** 获取审核状态样式类名 */
function getStatusClass(status) {
  const classMap = {
    0: 'status-pending',
    1: 'status-approved',
    2: 'status-rejected'
  }
  return classMap[status] || 'status-unknown'
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cc/review/export', {
    ...queryParams.value
  }, `review_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.status-pending {
  color: #e6a23c;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #fdf6ec;
  font-size: 12px;
}

.status-approved {
  color: #67c23a;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #f0f9eb;
  font-size: 12px;
}

.status-rejected {
  color: #f56c6c;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #fef0f0;
  font-size: 12px;
}

.status-unknown {
  color: #909399;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #f5f7fa;
  font-size: 12px;
}
</style>
